//
//  DataSummarySportCell.swift
//  TimeBoat
//
//  Created by bo wang on 2022/8/24.
//  Copyright © 2022 Time Boat. All rights reserved.
//

import UIKit

class DataSummarySportDayView: UIView {
    
    lazy var progressLayer: CAShapeLayer = {
        let l = CAShapeLayer()
        l.frame = bounds
        l.lineWidth = bounds.width
        l.lineCap = .round
        l.strokeColor = CGColor.hex(0x56B65E)
        l.strokeStart = 0
        l.strokeEnd = 0
        return l
    }()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        layer.masksToBounds = true
        layer.cornerRadius = bounds.width / 2
        layer.addSublayer(progressLayer)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        updateProgressLayerPath()
    }
    
    private func updateProgressLayerPath() {
        let path = UIBezierPath()
        path.move(to: bounds.bottomMid)
        path.addLine(to: bounds.topMid)
        progressLayer.path = path.cgPath
    }
    
    var progress: CGFloat = 0 {
        didSet {
            progressLayer.strokeEnd = max(0, min(1, progress))
        }
    }
}

class DataSummarySportWeekView: UIView {
    
    @IBOutlet var stackView: UIStackView!
    @IBOutlet var sportTimesLabel: UILabel!
    
    var dayViews: [DataSummarySportDayView] {
        stackView.arrangedSubviews.compactMap({ $0 as? DataSummarySportDayView })
    }
}

class DataSummarySportCell: DataSummaryCell {
    @IBOutlet var weekView: DataSummarySportWeekView!
    @IBOutlet var totalTimeLabel: UILabel!
    
    override func update(vm: DataSummaryCellViewModel, data: Any?) {
        super.update(vm: vm, data: data)
        
        guard let data = data as? SummaryDataModel,
              let sportInfo = data.weekSportInfo else {
            weekView.dayViews
                .enumerated()
                .forEach { elem in
                    let view = elem.element
                    view.progress = 0
                }
            
            weekView.sportTimesLabel.text = "运动0天，请继续加油"
            
            updateTotalTime(0)
            return
        }
        
        let totalTime = sportInfo.totalTime
        updateTotalTime(totalTime)

        let range = (0..<sportInfo.dayList.count)
        weekView.dayViews
            .enumerated()
            .forEach { elem in
                let view = elem.element
                if range ~= elem.offset {
                    let time = sportInfo.dayList[elem.offset].useTime
                    view.progress = time > 0 ? 1 : 0
                } else {
                    view.progress = 0
                }
            }
        
        let sportDayCounts = sportInfo.dayList.filter({ $0.useTime > 0 }).count
        weekView.sportTimesLabel.text = "运动\(sportDayCounts)天，请继续加油"
    }
    
    func updateTotalTime(_ time: Int) {
        if 0 == time {
            totalTimeLabel.text = "0秒"
            return
        }
        
        let hours = Int(time / 3600)
        let minutes = Int(time / 60) % 60
        let seconds = Int(time) % 60
        
        var compoents = [String]()
        if hours > 1 {
            compoents.append("\(hours)小时")
        }
        compoents.append("\(minutes)分")
        compoents.append("\(seconds)秒")
        totalTimeLabel.text = compoents.joined()
    }
}
